﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart.ViewModel
{
    public class ChooseFilterDlgViewModel : CCommandViewModel
    {
        SingleStartFolderItemViewModel _RootItem = null;
        SingleStartFolderItemViewModel _SelectedItem = null;
        string _sDataSource = "";
        string _SelectedValuePath = "";
        string _CurrentFilter = "";
        string _ActiveFilter = "";

        #region Initialize
        public ChooseFilterDlgViewModel(string dataSource, string Filter)
        {
            try
            {
                base.ViewName = LanguageStrings.GetText("Choose Filter");
                PropertyChanged += ChooseFilterDlgViewModel_PropertyChanged;
                DataSource = dataSource;

                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Choose"), new CDelegatingCommand(Delegate => this.Cmd_Exec_Choose())));
                m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.Cmd_Exec_Close())));
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on Initialize Dialog!", Ex, EMessageTypes.Error);
            }
        }
        #endregion

        void ChooseFilterDlgViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "DataSource")
                {
                    LoadDataSource(_sDataSource);
                }
                else if (e.PropertyName == "SelectedItem")
                {
                    if (_SelectedItem != null)
                        CurrentFilter = _SelectedItem.FolderPath;
                    else
                        CurrentFilter = "";
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on Change Property '" + e.PropertyName + "'!", Ex, EMessageTypes.Error);
            }
        }

        #region Properties

        public CSingleCommandViewModel Cmd_Close
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_Choose
        {
            get { return m_oCommands[0]; }
        }

        public string DataSource
        {
            get { return _sDataSource; }
            set
            {
                _sDataSource = value;
                OnPropertyChanged("DataSource");
            }
        }

        public SingleStartFolderItemViewModel RootItem
        {
            get { return _RootItem; }
            set
            {
                _RootItem = value;
                OnPropertyChanged("RootItem");
            }
        }

        public SingleStartFolderItemViewModel SelectedItem
        {
            get { return _SelectedItem; }
            set 
            {
                _SelectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        public string CurrentFilter
        {
            get { return _CurrentFilter; }
            protected set
            {
                _CurrentFilter = value;
                OnPropertyChanged("CurrentFilter");
            }
        }

        public string ActiveFilter
        {
            get { return _ActiveFilter; }            
        }

        #endregion

        #region Command Procedures 

        void Cmd_Exec_Choose()
        {
            try
            {
                ChooseFilterDlg ParentWindow = null;
                if ((ChooseFilterDlg)((CDelegatingCommand)Cmd_Choose.Command).CommandParameter != null)
                 {
                     ParentWindow = (ChooseFilterDlg)((CDelegatingCommand)Cmd_Choose.Command).CommandParameter;

                     if (ParentWindow != null)
                     {
                         _ActiveFilter = _CurrentFilter;
                         ParentWindow.DialogResult = true;
                         ParentWindow.Close();
                     }
                 }
            }
            catch (Exception Ex)
            {
                throw new Exception("Error while applying Data!", Ex);
            }

        }

        void Cmd_Exec_Close()
        {
            try
            {
                ChooseFilterDlg ParentWindow = null;
                if ((ChooseFilterDlg)((CDelegatingCommand)Cmd_Close.Command).CommandParameter != null)
                {
                    ParentWindow = (ChooseFilterDlg)((CDelegatingCommand)Cmd_Close.Command).CommandParameter;

                    if (ParentWindow != null)
                    {
                        ParentWindow.Close();
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Error while discard Data!", Ex);
            }
        }

        #endregion

        void LoadDataSource(string DataSource)
        {
            try
            {
                cStartmenuItem tmpRootItem = cMenuConfigReader.DataSources.GetData(DataSource, "");

                SingleStartFolderItemViewModel tmpFolderItem = null;
                tmpFolderItem = new SingleStartFolderItemViewModel(null, "Root", "Root");// new List<SingleStartFolderItemViewModel>());
                ////TreeViewItem oFolderItem = new TreeViewItem();
                ////oFolderItem.Name = "Root";
                ////oFolderItem.Header = "Root";
                ReadSubFolders(tmpRootItem, tmpFolderItem);

                RootItem = tmpFolderItem;
                RootItem.IsExpanded = true;

            }
            catch (Exception Ex)
            {
                throw new Exception("Unable to load DataSsource!", Ex);
            }
        }

        void ReadSubFolders(cStartmenuItem Parent, SingleStartFolderItemViewModel ItemParent)
        {
            try
            {
                if (Parent == null || ItemParent == null) return;

                Dictionary<string, Object>.KeyCollection oKeyItems = Parent.ChildItems.Keys;
                foreach (string KeyItem in oKeyItems)
                {
                    if (((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Folder ||
                        ((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Root ||
                        ((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemType == WMSSoft.Lib.Enums.eItemType.Filter)
                    {
                        SingleStartFolderItemViewModel tmpFolderItem = null;
                        string sParentPath = "";
                        if (Parent != null && Parent.FolderPath.Length > 0 && Parent.FolderPath.ToUpper() != "ROOT")
                            sParentPath = ItemParent.FolderPath + @"\";
                        tmpFolderItem = new SingleStartFolderItemViewModel(ItemParent, LanguageStrings.GetText(((cStartmenuItem)Parent.ChildItems[KeyItem]).ViewName),
                                                                           sParentPath + ((cStartmenuItem)Parent.ChildItems[KeyItem]).ItemName);
                        ItemParent.Items.Add(tmpFolderItem);
                        //Read all subfolders
                        if (((cStartmenuItem)Parent.ChildItems[KeyItem]).ChildItems.Count > 0)
                        {
                            ReadSubFolders((cStartmenuItem)Parent.ChildItems[KeyItem], tmpFolderItem);
                        }
                    }
                }

            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error while processing DataSource-Data - Folder!", Ex, EMessageTypes.Error);
            }
        }
    }
}
